/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pronostico.java
 *
 * Created on 12-may-2011, 12:59:24
 */

package logica;

import org.scilab.forge.jlatexmath.TeXConstants;
import org.scilab.forge.jlatexmath.TeXFormula;
import org.scilab.forge.jlatexmath.TeXIcon;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JLabel;


/**
 *
 * @author joshi
 */
public class pronostico extends javax.swing.JFrame {

    String math = "";
    String nombre = "";

     public static int xmes =0, xtmes=0;
     public static int tamano=0;

     

   

//    final public static Object datos[][] = { {"josue",1,"01,01,2010",275},
//                                {"josue",2,"03,01,2010",142},
//                                {"josue",3, "05,01,2010", 168},
//                                {"josue",5, "15,01,2010", 197},
//                                {"josue",1, "01,02,2010", 215},
//                                {"josue",2, "03,02,2010", 188},
//                                {"josue",6, "05,03,2010", 241},
//                                {"josue",23, "16,04,2010", 295},
//                                {"josue",1, "02,05,2010", 125},
//                                {"josue",5, "10,08,2010", 266},
//                                {"josue",9, "12,09,2010", 200},
//                                };


    public static Object datos[][] = new Object [100][4];
    /** Creates new form pronostico */
    public pronostico(String nombre) {
        initComponents();
        this.nombre = nombre;
        setIconImage(new ImageIcon(getClass().getResource("/img/doncito.png")).getImage());
        setLocationRelativeTo(null);
        Leer();
        tamano();
        if (tamano >0){
        Estadisticas();
        Series();
        pronosticos();
        }
        else{
        javax.swing.JOptionPane.showMessageDialog(null,"necesita mas datos para realizar un pronostivo valido");
        }
       // escribir();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jSplitPane1 = new javax.swing.JSplitPane();
        jgrafico = new javax.swing.JPanel();
        jdatos = new javax.swing.JPanel();
        jpuesto = new javax.swing.JLabel();
        jseries = new javax.swing.JPanel();
        jserie2 = new javax.swing.JPanel();
        jpronostico = new javax.swing.JPanel();
        jpro1 = new javax.swing.JLabel();
        jpro2 = new javax.swing.JLabel();
        jpro3 = new javax.swing.JLabel();
        jpro = new javax.swing.JLabel();
        jpro4 = new javax.swing.JLabel();
        jpro5 = new javax.swing.JLabel();
        jpro6 = new javax.swing.JLabel();
        jpro7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jSplitPane1.setDividerLocation(350);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        javax.swing.GroupLayout jgraficoLayout = new javax.swing.GroupLayout(jgrafico);
        jgrafico.setLayout(jgraficoLayout);
        jgraficoLayout.setHorizontalGroup(
            jgraficoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 690, Short.MAX_VALUE)
        );
        jgraficoLayout.setVerticalGroup(
            jgraficoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 349, Short.MAX_VALUE)
        );

        jSplitPane1.setTopComponent(jgrafico);

        jpuesto.setText("Puesto: Ganancia:");

        javax.swing.GroupLayout jdatosLayout = new javax.swing.GroupLayout(jdatos);
        jdatos.setLayout(jdatosLayout);
        jdatosLayout.setHorizontalGroup(
            jdatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jdatosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpuesto, javax.swing.GroupLayout.DEFAULT_SIZE, 554, Short.MAX_VALUE)
                .addGap(126, 126, 126))
        );
        jdatosLayout.setVerticalGroup(
            jdatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jdatosLayout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addComponent(jpuesto)
                .addContainerGap(57, Short.MAX_VALUE))
        );

        jSplitPane1.setRightComponent(jdatos);

        jTabbedPane1.addTab("Estadisticas", jSplitPane1);

        javax.swing.GroupLayout jseriesLayout = new javax.swing.GroupLayout(jseries);
        jseries.setLayout(jseriesLayout);
        jseriesLayout.setHorizontalGroup(
            jseriesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 692, Short.MAX_VALUE)
        );
        jseriesLayout.setVerticalGroup(
            jseriesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 478, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Serie Puesto", jseries);

        javax.swing.GroupLayout jserie2Layout = new javax.swing.GroupLayout(jserie2);
        jserie2.setLayout(jserie2Layout);
        jserie2Layout.setHorizontalGroup(
            jserie2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 692, Short.MAX_VALUE)
        );
        jserie2Layout.setVerticalGroup(
            jserie2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 478, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Serie Ganancia", jserie2);

        jpronostico.setBackground(new java.awt.Color(255, 255, 255));

        jpro1.setText("Se necesitan más datos");

        jpro2.setText("Se necesitan más datos");

        jpro3.setText("Se necesitan más datos");

        jpro.setText("Se necesitan más datos");

        jpro4.setText("Se necesitan más datos");

        jpro5.setText("Se necesitan más datos");

        jpro6.setText("Se necesitan más datos");

        jpro7.setText("Se necesitan más datos");

        javax.swing.GroupLayout jpronosticoLayout = new javax.swing.GroupLayout(jpronostico);
        jpronostico.setLayout(jpronosticoLayout);
        jpronosticoLayout.setHorizontalGroup(
            jpronosticoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpronosticoLayout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(jpronosticoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jpro4)
                    .addComponent(jpro7)
                    .addComponent(jpro6)
                    .addComponent(jpro5)
                    .addComponent(jpro)
                    .addComponent(jpro3)
                    .addComponent(jpro2)
                    .addComponent(jpro1))
                .addContainerGap(546, Short.MAX_VALUE))
        );
        jpronosticoLayout.setVerticalGroup(
            jpronosticoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpronosticoLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jpro)
                .addGap(18, 18, 18)
                .addComponent(jpro1)
                .addGap(18, 18, 18)
                .addComponent(jpro2)
                .addGap(18, 18, 18)
                .addComponent(jpro3)
                .addGap(49, 49, 49)
                .addComponent(jpro4)
                .addGap(18, 18, 18)
                .addComponent(jpro5)
                .addGap(18, 18, 18)
                .addComponent(jpro6)
                .addGap(18, 18, 18)
                .addComponent(jpro7)
                .addContainerGap(190, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("pronostico", jpronostico);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 697, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new pronostico("josue").setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JPanel jdatos;
    private javax.swing.JPanel jgrafico;
    private javax.swing.JLabel jpro;
    private javax.swing.JLabel jpro1;
    private javax.swing.JLabel jpro2;
    private javax.swing.JLabel jpro3;
    private javax.swing.JLabel jpro4;
    private javax.swing.JLabel jpro5;
    private javax.swing.JLabel jpro6;
    private javax.swing.JLabel jpro7;
    private javax.swing.JPanel jpronostico;
    private javax.swing.JLabel jpuesto;
    private javax.swing.JPanel jserie2;
    private javax.swing.JPanel jseries;
    // End of variables declaration//GEN-END:variables

    private void Estadisticas() {

        String aux = datos[tamano-1][3].toString();
        Double puesto = java.lang.Double.valueOf(aux);
        //puesto pu = new puesto(puesto ,nombre);
        puesto pu = new puesto(puesto ,nombre);
        this.jgrafico.setLayout(new BorderLayout());
        this.jgrafico.setVisible(true);
        this.jgrafico.add(pu.crearpanel());

        math = "Ganancia: \t"+datos[tamano-1][3]+" \\ Puesto: \t"+datos[tamano-1][1]+"";
        this.jpuesto.setIcon(get_latex(math));
    }

    private void Series() {
       // series se = new series(data,fecha);
        series se = new series("Serie de tiempo de puestos","Fecha","Puesto", 1, datos);
        this.jseries.setLayout(new BorderLayout());
        this.jseries.setVisible(true);
        this.jseries.add(se.createPanel2());

        
        series se1 = new series("Serie de tiempo de ganancias","Fecha","Ganancia",datos);
        this.jserie2.setLayout(new BorderLayout());
        this.jserie2.setVisible(true);
        this.jserie2.add(se1.createPanel());
    }

     private TeXIcon get_latex(String mat){
        TeXFormula fomule = new TeXFormula(mat);
        TeXIcon ti = fomule.createTeXIcon(
        TeXConstants.STYLE_DISPLAY, 18);
        BufferedImage b = new BufferedImage(ti.getIconWidth(), ti
            .getIconHeight(), BufferedImage.TYPE_INT_RGB);
        ti.paintIcon(new JLabel(), b.getGraphics(), 0, 0);

        return ti;
    }

    private void pronosticos() {
       
       int x = tamano;
       int h=1;
       math = "Ganancias";
       jpro.setIcon(get_latex(math));

       math = "Puestos";
       jpro4.setIcon(get_latex(math));

       if (x>1){

       Double dato1 =  java.lang.Double.valueOf(datos[x-1][3].toString());
       Double dato2 =  java.lang.Double.valueOf(datos[x-2][3].toString());
       Double dato4;
       Double dato3 = dato1 + (dato1-dato2);

       math = "\\hat{Y}_{t+1} = Y_{t}+(Y_{t} - Y_{t-1}) = "+ dato1 + " +("+ dato1 +" - "+dato2+") = "+dato3 + " ";
       jpro1.setIcon(get_latex(math));

       dato3=0.0;
       for(int i=tamano-1; i>0; i--){
           
             dato4 =  java.lang.Double.valueOf(datos[i][3].toString());
             if (i!=0){
                dato2 =  java.lang.Double.valueOf(datos[i-1][3].toString());
             }
             else{
                 dato2 = 0.0;
             }

             dato3 += dato4-dato2;

             if (datos[i][0]!=null){
                 h++;
             }
          
       }
       
       System.out.println(dato3);
       dato2= dato1+(dato3/h);
       math = "\\hat{Y}_{t+1} = Y_{t} + \\sum_{t=1}^{n} \\frac{(Y_{t} - Y_{t-1})}{n} = "+dato1+" "+"+\\frac{"+dato3+"}{"+h+"} = "+dato2+" " ;
       jpro2.setIcon(get_latex(math));

       dato3 = dato1*(dato1/dato2);
       math = "\\hat{Y}_{t+1} = Y_{t}\\frac{Y_{t}}{Y_{t-1}} = "+dato1 +"\\frac{"+dato1+"}{"+dato2+"} = "+dato3 + " ";
       jpro3.setIcon(get_latex(math));


       //puestos

        dato1 =  java.lang.Double.valueOf(datos[x-1][1].toString());
        dato2 =  java.lang.Double.valueOf(datos[x-2][1].toString());
        dato3 = dato1 + (dato1-dato2);

       math = "\\hat{Y}_{t+1} = Y_{t}+(Y_{t} - Y_{t-1}) = "+ dato1 + " +("+ dato1 +" - "+dato2+") = "+dato3 + " ";
       jpro5.setIcon(get_latex(math));

       dato3=0.0;
       h=1;
       for(int i=tamano-1; i>0; i--){

             dato4 =  java.lang.Double.valueOf(datos[i][1].toString());
             if (i!=0){
                dato2 =  java.lang.Double.valueOf(datos[i-1][1].toString());
             }
             else{
                 dato2 = 0.0;
             }

             dato3 += dato4-dato2;
             if(datos[i][0] !=null){
                 h++;
             }

       }

       System.out.println(dato3);
       dato2= dato1+(dato3/h);
       math = "\\hat{Y}_{t+1} = Y_{t} + \\sum_{t=1}^{n} \\frac{(Y_{t} - Y_{t-1})}{n} = "+dato1+" "+"+\\frac{"+dato3+"}{"+h+"} = "+dato2+" " ;
       jpro6.setIcon(get_latex(math));

       dato3 = dato1*(dato1/dato2);
       math = "\\hat{Y}_{t+1} = Y_{t}\\frac{Y_{t}}{Y_{t-1}} = "+dato1 +"\\frac{"+dato1+"}{"+dato2+"} = "+dato3 + " ";
       jpro7.setIcon(get_latex(math));


       }





    }

    private void set_cursorw(){
        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    }
    private void set_cursor(){
        this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }

    private void Leer() {
        File archivo = null;
      FileReader fr = null;
      BufferedReader br = null;

      try {
         // Apertura del fichero y creacion de BufferedReader para poder
         // hacer una lectura comoda (disponer del metodo readLine()).
         archivo = new File ("datos.txt");
         fr = new FileReader (archivo);
         br = new BufferedReader(fr);

         //josue,1,01,01,2010,275


         // Lectura del fichero
         String linea;
         int cont=0;
         String vec[] = null;
         while((linea=br.readLine())!=null){
            
            vec = linea.split(",");
            
                if (vec[0].compareTo(nombre)==0){
                    datos[cont][0]=vec[0];
                    datos[cont][1]=vec[1];
                    datos[cont][2]=vec[2]+","+vec[3]+","+vec[4];
                    datos[cont][3]=vec[5];
                    cont++;
                }
            

          }

      }
      catch(Exception e){
         e.printStackTrace();
      }finally{
         // En el finally cerramos el fichero, para asegurarnos
         // que se cierra tanto si todo va bien como si salta
         // una excepcion.
         try{
            if( null != fr ){
               fr.close();
            }
         }catch (Exception e2){
            e2.printStackTrace();
         }
      }
    }

    public void escribir(){

        FileWriter fichero = null;
        PrintWriter pw = null;
        try
        {
            fichero = new FileWriter("prueba.txt");
            pw = new PrintWriter(fichero);

            for (int i = 0; i < 10; i++)
                pw.println("Linea " + i);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
           try {
           // Nuevamente aprovechamos el finally para
           // asegurarnos que se cierra el fichero.
           if (null != fichero)
              fichero.close();
           } catch (Exception e2) {
              e2.printStackTrace();
           }
        }


    }

    private void tamano() {

        for(int i=0; i<datos.length;i++){
            if (datos[i][0]==null){
                break;
            }
            tamano++;
        }
        System.out.println(tamano);
    }

   

}

